// Cookie Manager
class CookieManager {
    constructor() {
        this.showConsentBanner();
        this.loadPreferences();
    }

    showConsentBanner() {
        if (!localStorage.getItem('cookieConsent')) {
            const banner = document.createElement('div');
            banner.className = 'fixed bottom-0 left-0 right-0 bg-gray-800 p-4 z-50';
            banner.innerHTML = `
                <div class="container mx-auto flex flex-col md:flex-row items-center justify-between">
                    <p class="text-gray-300 mb-4 md:mb-0">We use cookies to enhance your experience. By continuing to visit this site you agree to our use of cookies.</p>
                    <div class="flex space-x-4">
                        <button onclick="cookieManager.acceptCookies()" class="bg-blue-600 hover:bg-blue-700 text-white px-4 py-2 rounded">Accept All</button>
                        <button onclick="cookieManager.showPreferences()" class="bg-gray-600 hover:bg-gray-700 text-white px-4 py-2 rounded">Preferences</button>
                        <button onclick="cookieManager.rejectCookies()" class="bg-gray-600 hover:bg-gray-700 text-white px-4 py-2 rounded">Reject All</button>
                    </div>
                </div>
            `;
            document.body.appendChild(banner);
        }
    }

    acceptCookies() {
        localStorage.setItem('cookieConsent', 'accepted');
        const preferences = {
            analytics: true,
            preferences: true,
            marketing: true
        };
        localStorage.setItem('cookiePreferences', JSON.stringify(preferences));
        this.removeBanner();
    }

    rejectCookies() {
        localStorage.setItem('cookieConsent', 'rejected');
        const preferences = {
            analytics: false,
            preferences: false,
            marketing: false
        };
        localStorage.setItem('cookiePreferences', JSON.stringify(preferences));
        this.removeBanner();
    }

    showPreferences() {
        window.location.href = '/cookie-policy.html';
    }

    removeBanner() {
        const banner = document.querySelector('.fixed.bottom-0');
        if (banner) {
            banner.remove();
        }
    }

    loadPreferences() {
        const savedPreferences = localStorage.getItem('cookiePreferences');
        if (savedPreferences) {
            return JSON.parse(savedPreferences);
        }
        return null;
    }

    isAnalyticsAllowed() {
        const preferences = this.loadPreferences();
        return preferences && preferences.analytics;
    }

    isPreferencesAllowed() {
        const preferences = this.loadPreferences();
        return preferences && preferences.preferences;
    }

    isMarketingAllowed() {
        const preferences = this.loadPreferences();
        return preferences && preferences.marketing;
    }
}

// Initialize cookie manager
const cookieManager = new CookieManager();
